package com.mysteel.seckill.interceptor;

import com.mysteel.common.constant.AuthServerConstant;
import com.mysteel.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Set;

/**
 * ProjectName: mail
 * ClassName: LoginUserInteceptor
 * Description:
 * date: 2021/03/20/020 14:21
 *
 * @author xiaochao
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        AntPathMatcher pathMatcher = new AntPathMatcher();
        if (pathMatcher.match("/kill",request.getRequestURI())){
            HttpSession session = request.getSession();
            Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER_DATA);
            if (attribute != null){
                // 用户已登录
                MemberResponseVo memberResponseVo = (MemberResponseVo) attribute;
                threadLocal.set(memberResponseVo);
                return true;
            }
            session.setAttribute("msg","请先登录");
            response.sendRedirect("http://auth.xcmail.com/login.html");
            return false;
        }
        return true;
    }
}
